<!--
 * @Author: luosi 1297957337@qq.com
 * @Date: 2024-11-20 17:27:35
 * @LastEditors: huang liang miao 1127255342@qq.com
 * @LastEditTime: 2024-12-18 08:42:36
 * @FilePath: \sr_system_manager_ui\src\views\achievementReward\researchStat\index.vue
 * @Description: 科研统计列表
-->
<template>
  <div class="research-stat-container">
    <div class="search" v-if="showSearch">
      <SearchBar ref="searchBarRef" :components="conditionComponents" :hideRightArea="false" @search="refreshTableData" />
    </div>
    <div class="list">
      <TableForm ref="tableRef" :auto-height="true" :data="dataList" :columns="columns" :tableLoading="loading" :total-count="total"
        v-model:showSearch="showSearch" :showRowSelection="true" :showPagination="true" :showToolBar="true" :right-first-btn-permission="['achievementReward:researchStat:export']"
        :download="(params) => { exportListData(params) }" @refresh="refreshTableData">
        <template #default="{ record }">
          <el-button link type="primary" @click="handleDetail(record.id, record.achvType)">查看</el-button>
        </template>
      </TableForm>
    </div>
  </div>
</template>

<script setup lang="ts">
import { achvCommonPage, exportResearchStatisticsApi } from '@/api/resultsManagement';
import { AchvInfoPageVO, PageBeanAchvInfoPageVO } from '@/api/resultsManagement/types';
import formateDate from '@/utils/formateDate';
import { dictEnum } from '@/utils/constEnum';
import { achvTypeMap } from '@/utils/constText';
import { downloadFile } from '@/utils/download'

const { proxy } = getCurrentInstance()

const dict = proxy.useDict(dictEnum.department)

const router = useRouter()
const searchBarRef = ref()
const showSearch = ref(true)
// 请求参数
const parameters = ref({
  page: 1,
  size: 20,
  statisticalYear: '',
  departmentId: '', // 学院（部）
  achvType: '',
  achvDateArray: [],
  businessStartDate: '',
  businessEndDate: '',
  name: undefined, // 工号/姓名
})

const conditionComponents = computed(() => [
  {
    type: 2,
    key: 'statisticalYear',
    placeholder: '统计年度',
    width: '220px',
    config: {
      type: 'year',
      valueFormat: 'YYYY'
    }
  },
  {
    type: 1,
    key: 'departmentId',
    placeholder: '学院（部）',
    width: '220px',
    config: {
      options: dict[dictEnum.department]
    }
  },
  {
    type: 1,
    key: 'achvType',
    placeholder: '成果类型',
    width: '220px',
    config: {
      options: [
        { title: '学术论文', value: 0 },
        { title: '学术著作', value: 1 },
        { title: '专利、软著', value: 2 },
        { title: '纵向项目', value: 3 },
        { title: '横向项目', value: 4 },
        { title: '指导学生科研项目结项', value: 5 },
        { title: '重点实验室/科研团队', value: 6 },
        { title: '科研获奖', value: 7 },
        { title: '体育竞赛', value: 8 },
        { title: '其他成果', value: 9 },
      ]
    }
  },
  {
    type: 2,
    key: 'achvDateArray',
    width: '220px',
    config: {
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
    }
  },
  {
    type: 0,
    key: 'name',
    placeholder: '成果名称',
    width: '220px',
  }
])

const tableRef = ref()
const dataList = ref<AchvInfoPageVO[]>([])
const total = ref(0)
const columns = ref([
  {
    title: '序号',
    width: 48,
    dataIndex: 'index',
    formatter: (row: any, column: any, cellValue: any, index: number) => {
      return index + 1
    }
  },
  {
    title: '所属院校（部）',
    width: 120,
    dataIndex: 'departmentName',
  },
  {
    title: '工号',
    width: 120,
    dataIndex: 'accountCode',
  },
  {
    title: '姓名',
    width: 80,
    dataIndex: 'accountName',
  },
  {
    title: '成果名称',
    minWidth: 200,
    dataIndex: 'name',
  },
  {
    title: '类别',
    width: 200,
    dataIndex: 'achvType',
    formatter: (row) => achvTypeMap[row.achvType as keyof typeof achvTypeMap]
  },
  {
    title: '科研分',
    width: 80,
    dataIndex: 'researchScore',
  },
  {
    title: '统计年度',
    width: 100,
    dataIndex: 'statisticalYear',
    formatter: (row) => `${row.statisticalYear}年度`
  },
  {
    title: '成果时间',
    width: 100,
    dataIndex: 'businessDate',
    formatter: (row) => formateDate(row.businessDate).format('YYYY-MM-DD')
  },
  {
    title: '操作',
    width: 100,
    dataIndex: 'default',
    slotName: 'default',
  }
])

const { run: getData, loading } = useRequest(achvCommonPage, {
  manual: true,
  onSuccess: (res: PageBeanAchvInfoPageVO) => {
    const { content, totalElements } = res || {}
    dataList.value = content || []
    total.value = totalElements || 0
  }
})

/* 导出列表数据 */
const { runAsync: exportFile } = useRequest(exportResearchStatisticsApi, {
  // false 为自动执行
  manual: true,
  // 请求成功
  onSuccess: () => {
  },
  // 请求失败
  onError: () => {
  },
})
// 导出
const exportListData = async (params) => {
  const data = {
    ...parameters.value,
    businessStartDate: parameters.value.achvDateArray?.[0],
    businessEndDate: parameters.value.achvDateArray?.[1],
    idList: params,
  }
  const res = await exportFile(data)
  downloadFile(res)
}

const handleDetail = (id: string, achvType: number) => {
  const achvTypePathMap = {
    0: '/resultsManagement/office/office-academic-paper/detail', // 学术论文
    1: '/resultsManagement/office/office-academic-work/detail', // 学术著作
    2: '/resultsManagement/office/office-patent/detail', // 专利、成果转让
    3: '/resultsManagement/office/office-vertical/detail', // 纵向结项
    4: '/resultsManagement/office/office-horizontal/detail', // 横向项目
    5: '/resultsManagement/office/office-guide-student/detail', // 指导学生科研项目结项
    6: '/resultsManagement/office/office-laboratory/detail', // 重点实验室、科研团队立项
    7: '/resultsManagement/office/office-research-award/detail', // 科研获奖
    8: '/resultsManagement/office/office-sports-competition/detail', // 体育竞赛
    9: '/resultsManagement/office/office-other/detail', // 其他成果
  }
  router.push({ path: achvTypePathMap[achvType as keyof typeof achvTypePathMap], query: { id } })
}

const refreshTableData = (params) => {
  parameters.value = { ...parameters.value, ...params }
  getData({
    ...parameters.value,
    statisticalYear: parameters.value.statisticalYear,
    departmentId: parameters.value.departmentId,
    achvType: parameters.value.achvType,
    businessStartDate: parameters.value.achvDateArray?.[0],
    businessEndDate: parameters.value.achvDateArray?.[1],
    name: parameters.value.name
  })
}

onMounted(() => {
  tableRef.value?.refreshInitPage()
})
</script>

<style scoped lang="scss">
.research-stat-container {
  --at-apply: flex-col space-y-[16px] h-full;

  .search {
    --at-apply: px-[20px] py-[16px] bg-white rounded-[4px];
  }

  .list {
    --at-apply: flex-1 px-[20px] pb-[20px] bg-white rounded-[4px];
  }
}
</style>